package org.qydjk.common.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeanMapUtil {

    /**
     * 从map集合中获取属性值
     * @param map
     * @param key
     * @param defaultValue
     * @param <E>
     * @return
     */
    public final static <E> E get(Map map, Object key, E defaultValue) {
        Object o = map.get(key);
        if (o == null)
            return defaultValue;
        return (E) o;
    }

    /**
     * Map集合对象转化成 JavaBean集合对象
     * @param javaBean
     * @param mapList
     * @param needUnderscoreName 是否需要驼峰转下划线，数据库相关的需要
     * @param <T>
     * @return
     */
    public static <T> List<T> map2Java(Class javaBean, List<Map<String,Object>> mapList,boolean needUnderscoreName) {
        if(mapList == null || mapList.isEmpty()){
            return new ArrayList<T>();
        }
        List<T> objectList = new ArrayList<T>();

        T object = null;
        for(Map map : mapList){
            if(map != null){
                object = map2Java(javaBean, map, needUnderscoreName);
                objectList.add(object);
            }
        }

        return objectList;
    }

    /**
     * Map对象转化成 JavaBean对象
     * @param javaBean
     * @param map
     * @param needUnderscoreName 是否需要驼峰转下划线，数据库相关的需要
     * @param <T>
     * @return
     */
    public static <T> T map2Java(Class javaBean, Map map, boolean needUnderscoreName) {
        try{
            BeanInfo beanInfo = Introspector.getBeanInfo(javaBean); // 获取类属性
            Object obj = javaBean.newInstance(); // 创建 JavaBean 对象

            // 给 JavaBean 对象的属性赋值
            PropertyDescriptor[] propertyDescriptors = beanInfo
                    .getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();
                if(needUnderscoreName){
                    propertyName = StringUtil.underscoreName(propertyName);
                }

                if (map.containsKey(propertyName)) {
                    // 下面一句可以 try 起来，这样当一个属性赋值失败的时候就不会影响其他属性赋值。
                    Object value = map.get(propertyName);

                    Object[] args = new Object[1];
                    args[0] = value;

                    descriptor.getWriteMethod().invoke(obj, args);
                }
            }
            return (T) obj;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * JavaBean对象转化成Map对象
     * @param javaBean
     * @return
     */
    public static Map java2Map(Object javaBean) {
        Map map = new HashMap();

        try {
            // 获取javaBean属性
            BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());

            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            if (propertyDescriptors != null && propertyDescriptors.length > 0) {
                String propertyName = null; // javaBean属性名
                Object propertyValue = null; // javaBean属性值
                for (PropertyDescriptor pd : propertyDescriptors) {
                    propertyName = pd.getName();
                    if (!propertyName.equals("class")) {
                        Method readMethod = pd.getReadMethod();
                        propertyValue = readMethod.invoke(javaBean, new Object[0]);
                        map.put(propertyName, propertyValue);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

}